<template>
	<div class="system-edit-role-container">
		<el-dialog title="修改角色" v-model="isShowDialog" width="600px">
			<el-form :model="ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="角色名称">
							<el-input v-model="ruleForm.name" placeholder="请输入角色名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="角色编码">
							<el-input v-model="ruleForm.code" placeholder="请输入角色标识" clearable></el-input>
						</el-form-item>
					</el-col>
					
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="角色描述">
							<el-input v-model="ruleForm.remark" type="textarea" placeholder="请输入角色描述" maxlength="150"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">修 改</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, defineComponent ,} from 'vue';
import { rolesApi } from '/@/api/system/role'
import { ElMessage } from 'element-plus'




interface DialogRow {
	name: string;
	code: string;
	id: number;
	remark: string;
}

interface RoleState {
	isShowDialog: boolean;
	ruleForm: DialogRow;
}

export default defineComponent({
	name: 'systemEditRole',
	setup() {
		const state = reactive<RoleState>({
			isShowDialog: false,
			ruleForm: {
				name: '', // 角色名称
				code: '', // 角色标识
				id: 0, // 排序
				remark: '', // 角色描述
			}
		});

		const roleSave = async()=> {
			return await rolesApi().save(state.ruleForm)
		}

		// 打开弹窗
		const openDialog = (row: DialogRow) => {
			state.ruleForm = row;
			state.isShowDialog = true;
		};
		// 关闭弹窗
		const closeDialog = () => {
			state.isShowDialog = false;
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 新增
		const onSubmit = async() => {

			let {code,msg} = await roleSave()
			if ( code == 0 ) {
				ElMessage.success(msg)
				closeDialog();
			} else {
				ElMessage.error(msg)
			}
			closeDialog();
		};
		
		return {
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			...toRefs(state),
		};
	},
});
</script>

<style scoped lang="scss">
.system-edit-role-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
